package logic

import (
	"context"
	"database/sql"
	"goods/goods"
	"goods/internal/model"
	"goods/internal/svc"
	"log"
	"strconv"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateGoodsLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateGoodsLogic {
	return &UpdateGoodsLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateGoodsLogic) UpdateGoods(in *goods.UpdateGoodsRequest) (*goods.UpdateGoodsResponse, error) {
	// todo: add your logic here and delete this line
	price, err := strconv.ParseFloat(in.Data.Price, 64)
	if err != nil {
		log.Println("类型转换失败")
		return &goods.UpdateGoodsResponse{Code: -1, Msg: "类型转换失败"}, nil
	}
	goodsmsg := &model.Goods{
		Name:        in.Data.Name,
		CategoryId:  in.Data.CategoryId,
		Price:       sql.NullFloat64{Float64: price},
		Image:       sql.NullString{String: in.Data.Image, Valid: true},
		Description: sql.NullString{String: in.Data.Description, Valid: true},
		Status:      int64(in.Data.Status),
		UpdateTime:  sql.NullTime{Time: time.Now(), Valid: true},
	}
	err = l.svcCtx.Goods.Update(context.Background(), goodsmsg)
	if err != nil {
		return &goods.UpdateGoodsResponse{Code: -1, Msg: "失败的更新"}, err
	}
	return &goods.UpdateGoodsResponse{Code: 200, Msg: "更新成功"}, nil

}
